package com.example.demo.beans;

import com.example.demo.user.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 东莞呵呵
 * Date:2022-10-16
 * Time:19:07
 */
@Slf4j
@Controller
@ResponseBody
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserInfo userInfo;

    @Value("${img.path}")
    private String path;

    @RequestMapping("/sayhi")
    public String sayHi1() {
        return "你好，张嘉庆！";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/sayhi2")
    public String sayHi2() {
        return "Hello World！";
    }

    //伪代码
    @RequestMapping("/getuserbyid")
    public UserInfo getUserById(@RequestParam("uid") Integer userId) {
        userInfo.setId(userId == null ? 0 : userId);
        userInfo.setUsername("张三");
        userInfo.setAge(18);
        return userInfo;
    }

    @RequestMapping("/reg")
    public String reg(@RequestBody UserInfo userInfo) {
        return "用户信息： " + userInfo;
    }

    @RequestMapping("/getuser/{id}/{name}")
    public String GetUser(@PathVariable UserInfo userInfo) {
        return "用户信息： " + userInfo;
    }


    @RequestMapping("/upimg")
    public boolean upImg(Integer id, @RequestPart("img") MultipartFile file) {

        //得到原图的名称
        String fileName = file.getOriginalFilename();
        //得到后缀
        fileName = fileName.substring(fileName.lastIndexOf("."));
        //命名图片
        fileName = UUID.randomUUID().toString() + fileName;

        boolean result = false;
        try {
            file.transferTo(new File(path + fileName));
            result = true;
        } catch (IOException e) {
            log.error("上传图片失败： " + e.getMessage());
        }
        return result;
    }

    @RequestMapping("/cookie")
    public String getCookie(@CookieValue("bite") String cookie) {
        return "Cookie Value:" + cookie;
    }

    @RequestMapping("/cookie2")
    public void getCookie2(HttpServletRequest req) {
        Cookie[] cookies = req.getCookies();
        for (Cookie item : cookies) {
            log.info("Cookie Name: " + item.getName() +
                    "| Cookie Value: " + item.getValue());
        }
    }

    @RequestMapping("/getua")
    public String getHead(HttpServletRequest req) {
        return "header: " + req.getHeader("User-Agent");
    }

    @RequestMapping("/getua2")
    public String getHead(@RequestHeader("User-Agent") String header) {
        return "Header: " + header;
    }

    @RequestMapping("/setsess")
    public boolean setSession(HttpServletRequest req) {
        boolean result = false;
        // 1.得到 HttpSession
        HttpSession session = req.getSession(true); // true=如果没有会话，那么创建一个会话
        // 2.使用 setAtt 设置值
        session.setAttribute("userinfo", "张三");
        result = true;
        return result;
    }

    @RequestMapping("/getsess")
    public String getSession(HttpServletRequest req) {
        String result = null;
        //1、得到HttpSession对象
        HttpSession session = req.getSession(false);//设置为false如果有会话，就是用会话如果没有不会创建会话
        //2、getAtt得到Session信息
        if (session != null && session.getAttribute("userinfo") != null) {
            result = (String) session.getAttribute("userinfo");
        }
        return result;
    }
    @RequestMapping("/getsess2")
    public String getSession(@SessionAttribute(value = "userinfo",required = false)String userinfo) {
        return "会话： "+userinfo;
    }
}
